//
//  PersonalCentreAddressManageViewController.m
//  XingMei
//
//  Created by 姚琪 on 2019/3/26.
//  Copyright © 2019年 钱金军. All rights reserved.
//

#import "PersonalCentreAddressManageViewController.h"
#import "AddressManageCell.h"
#import "AddAddressViewController.h"
#import "EditShippingAdressViewController.h"//编辑收货地址

static NSString *normalAddressIdentefier = @"normalAddress";

@interface PersonalCentreAddressManageViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger isDefault;
    UIImageView *errImg;
    int page;
}
@property(nonatomic,strong)UITableView *chooseAddressTableV;
@property(nonatomic,strong)NSMutableArray *addressArray;
@end

@implementation PersonalCentreAddressManageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    [self setNavTitleText:@"地址管理"];
    [self initTableView];
    [self initLeftViewButton];
    [self initBottomView];
    // Do any additional setup after loading the view.
}
-(void)initTableView{
    self.chooseAddressTableV = [[UITableView alloc]initWithFrame:CGRectMake(0, Height_NavBar -1, KScreenWidth, KScreenHeight- Height_NavBar-50 -Height_Indicator - 60) style:UITableViewStyleGrouped];;
    self.chooseAddressTableV.delegate = self;
    self.chooseAddressTableV.dataSource = self;
    self.chooseAddressTableV.showsVerticalScrollIndicator = NO;
    self.chooseAddressTableV.showsHorizontalScrollIndicator = NO;
    self.chooseAddressTableV.backgroundColor = [UIColor whiteColor];
//    self.chooseAddressTableV.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headData)];
//    self.chooseAddressTableV.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footData)];
//    [self.chooseAddressTableV.mj_header beginRefreshing];
    
    [self.chooseAddressTableV registerClass:[AddressManageCell class] forCellReuseIdentifier:normalAddressIdentefier];
    errImg = [self addNoneGoodslist];
    errImg.topv = 40;
    [self.chooseAddressTableV addSubview:errImg];
    
    [self.view addSubview:self.chooseAddressTableV];
    
}
-(void)initBottomView{
    UIButton *addAddressBtn = [[UIButton alloc]initWithFrame:CGRectMake(50,KScreenHeight - Height_Indicator - 44 - 60 , KScreenWidth - 100,44)];
    
    [addAddressBtn.layer addSublayer:[self setButtonLayer:addAddressBtn]];
    [addAddressBtn setTitle:@"保存" forState:0];
    [addAddressBtn setTitleColor:[UIColor whiteColor] forState:0];
    addAddressBtn.titleLabel.font = BoldFont(18);
    addAddressBtn.layer.cornerRadius = 22;
    addAddressBtn.layer.masksToBounds = YES;
    [addAddressBtn addTarget:self action:@selector(showAddGoodsAddressesController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addAddressBtn];
}
-(void)initLeftViewButton{
    UIButton *addAddressBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 54 - 10,Height_StatusBar, 54,44)];
    [addAddressBtn setImage:[UIImage imageNamed:@"redact_icon_1"] forState:0];
    [addAddressBtn addTarget:self action:@selector(showAddGoodsAddressesController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addAddressBtn];

}
-(void)showAddGoodsAddressesController{
    AddAddressViewController *controller = [[AddAddressViewController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}
-(void)headData{
    page = 1;
    [self request:1];
}
-(void)footData{
    page ++ ;
    [self request:0];
    
}
-(void)request:(int)tag{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@"10" forKey:@"pageSize"];
    [dict setObject:[NSString stringWithFormat:@"%d",page] forKey:@"pageNum"];
    [ZWJNetworkManager requestWithRequestData:dict WithUrl:@"" WithWithCompletionBlockWithSuccess:^(NSDictionary *dic) {
        [self.chooseAddressTableV.mj_header endRefreshing];
        [self.chooseAddressTableV.mj_footer endRefreshing];
        if ([dic[Code] intValue] == 200) {
            if (tag == 1) {
                self.addressArray = [[NSMutableArray alloc]init];
            }
            [self.addressArray addObjectsFromArray:dic[Response]];
            
            [self.chooseAddressTableV reloadData];
        }
        
    } failure:^(id error) {
        [self.chooseAddressTableV.mj_header endRefreshing];
        [self.chooseAddressTableV.mj_footer endRefreshing];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    return self.addressArray.count;
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.addressArray[indexPath.row];
    AddressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:normalAddressIdentefier];
    if (indexPath.row == 0) {
        cell.isDefultAddress = YES;
    }else{
        cell.isDefultAddress = NO;
    }
    cell.editorBtn.tag = indexPath.row;
    [cell.editorBtn addTarget:self action:@selector(editorAddress:) forControlEvents:UIControlEventTouchUpInside];
    cell.dict = @{@"consignee":@"钱胜强",@"phone":@"15701212365",@"fullAddress":@"浙江省 嘉兴市秀洲区秀洲区中禾广中禾广场1-2009室"};;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.addressArray[indexPath.section];
    AddressManageCell *cell = [tableView dequeueReusableCellWithIdentifier:normalAddressIdentefier];
    cell.dict = @{@"consignee":@"钱胜强",@"phone":@"15701212365",@"fullAddress":@"浙江省 嘉兴市秀洲区秀洲区中禾广中禾广场1-2009室"};
    return cell.height;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc]init];
    return view;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}
-(void)editorAddress:(UIButton *)sender{
    //通过button的下标拿到对应的dict
    EditShippingAdressViewController *controller = [[EditShippingAdressViewController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
    
}
-(void)setDefaultAddressRequest:(NSString *)str{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:str forKey:@"addressId"];
    [ZWJNetworkManager requestWithRequestData:dict WithUrl:@"" WithWithCompletionBlockWithSuccess:^(NSDictionary *dic) {
        if ([dic[Code] intValue] == 200) {
            [self showWithTitle:dic[Response]];
            [self headData];
        }
    } failure:^(id error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(CAGradientLayer *)setButtonLayer:(UIView *)view{
    CAGradientLayer *gradientLayer0 = [[CAGradientLayer alloc] init];
    gradientLayer0.frame = view.bounds;
    gradientLayer0.colors = @[
                              (id)[UIColor colorWithRed:255.0f/255.0f green:177.0f/255.0f blue:26.0f/255.0f alpha:1.0f].CGColor,
                              (id)[UIColor colorWithRed:255.0f/255.0f green:108.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                              (id)[UIColor colorWithRed:255.0f/255.0f green:87.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor,
                              (id)[UIColor colorWithRed:255.0f/255.0f green:66.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor];
    gradientLayer0.locations = @[@0, @1, @1, @1];
    [gradientLayer0 setStartPoint:CGPointMake(0, 1)];
    [gradientLayer0 setEndPoint:CGPointMake(1, 1)];
    return gradientLayer0;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
